package tk.pak0.audiomidifier.model.runnable;

public class ThreadControl {

	private static ThreadControl instance=null;
	
	private SoundReader soundReader;
	private PitchProcessor pitchProcessor;
	private MidiListener midiListener;
	private Metronome metronome;
	
	public static ThreadControl getInstance(){
		if(instance == null){
			instance = new ThreadControl();
		}
		return instance;
	}
	private ThreadControl(){
		soundReader = new SoundReader();
		pitchProcessor = new PitchProcessor();
		midiListener = new MidiListener();
		metronome = new Metronome();
		
		soundReader.setPitchThread(pitchProcessor);
		pitchProcessor.setMidiThread(midiListener);
	}
	
	public void setMetronome(boolean state){
		if(state)
			metronome = new Metronome();
		else
			metronome = null;
	}
	
	public void fireThreads(){
		Thread soundThread = new Thread(soundReader);
		Thread pitchProcessThread = new Thread(pitchProcessor);
		Thread midiThread = new Thread(midiListener);
		Thread metronomeThread;
		soundThread.start();
		midiThread.start();
		pitchProcessThread.start();
		
		if(metronome != null){
			metronomeThread = new Thread(metronome);
			metronomeThread.start();
		}
	}
	
	public void killThreads(){
		soundReader.kill();
		pitchProcessor.kill();
		midiListener.kill();
		if(metronome != null){
			metronome.kill();
		}
	}
	
	public SoundReader getSoundReader() {
		return soundReader;
	}
	public void setSoundReader(SoundReader soundReader) {
		this.soundReader = soundReader;
	}
	public PitchProcessor getPitchProcessor() {
		return pitchProcessor;
	}
	public void setPitchProcessor(PitchProcessor pitchProcessor) {
		this.pitchProcessor = pitchProcessor;
	}
	public MidiListener getMidiListener() {
		return midiListener;
	}
	public void setMidiListener(MidiListener midiListener) {
		this.midiListener = midiListener;
	}
	public Metronome getMetronome() {
		return metronome;
	}
	public void setMetronome(Metronome metronome) {
		this.metronome = metronome;
	}
}
